﻿using Caliburn.Micro;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Models;
using SD.IOC.Core.Mediators;
using SlamDunk.MES.Client.ViewModels.Facility;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using System.Windows;

namespace SlamDunk.MES.Client.ViewModels.EquipmentAsset
{
    /// <summary>
    /// 设备资产创建视图模型
    /// </summary>
    public class AddViewModel : ScreenBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 计划管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IMaintenanceContract> _maintenanceContract;

        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public AddViewModel(ServiceProxy<IMaintenanceContract> maintenanceContract, IWindowManager windowManager)
        {
            this._maintenanceContract = maintenanceContract;
            this._windowManager = windowManager;
        }

        #endregion

        #region # 属性

        #region 设备资产编号 —— string EquipmentAssetNo
        /// <summary>
        /// 设备资产编号
        /// </summary>
        [DependencyProperty]
        public string EquipmentAssetNo { get; set; }
        #endregion

        #region 设备资产名称 —— string EquipmentAssetName
        /// <summary>
        /// 设备资产名称
        /// </summary>
        [DependencyProperty]
        public string EquipmentAssetName { get; set; }
        #endregion

        #region 已选设备 —— EquipmentInfo SelectedEquipment
        /// <summary>
        /// 已选设备
        /// </summary>
        [DependencyProperty]
        public EquipmentInfo SelectedEquipment { get; set; }
        #endregion

        #region 生产日期 —— DateTime? ProducedDate
        /// <summary>
        /// 生产日期
        /// </summary>
        [DependencyProperty]
        public DateTime? ProducedDate { get; set; }
        #endregion

        #region 采购日期 —— DateTime? PurchasedDate
        /// <summary>
        /// 采购日期
        /// </summary>
        [DependencyProperty]
        public DateTime? PurchasedDate { get; set; }
        #endregion

        #region 启用日期 —— DateTime? EnabledDate
        /// <summary>
        /// 启用日期
        /// </summary>
        [DependencyProperty]
        public DateTime? EnabledDate { get; set; }
        #endregion

        #region 制造商 —— string Manufacturer
        /// <summary>
        /// 制造商
        /// </summary>
        [DependencyProperty]
        public string Manufacturer { get; set; }
        #endregion

        #region 规格 —— string Specification
        /// <summary>
        /// 规格
        /// </summary>
        [DependencyProperty]
        public string Specification { get; set; }
        #endregion

        #region 型号 —— string ModelNo
        /// <summary>
        /// 型号
        /// </summary>
        [DependencyProperty]
        public string ModelNo { get; set; }
        #endregion

        #region 价值 —— decimal? Worth
        /// <summary>
        /// 价值
        /// </summary>
        [DependencyProperty]
        public decimal? Worth { get; set; }
        #endregion

        #region 使用年限 —— string ServiceLife
        /// <summary>
        /// 使用年限
        /// </summary>
        [DependencyProperty]
        public string ServiceLife { get; set; }
        #endregion

        #region 折旧年限 —— string DepreciableLife
        /// <summary>
        /// 折旧年限
        /// </summary>
        [DependencyProperty]
        public string DepreciableLife { get; set; }
        #endregion

        #region 售后电话 —— string SupportPhone
        /// <summary>
        /// 售后电话
        /// </summary>
        [DependencyProperty]
        public string SupportPhone { get; set; }
        #endregion

        #region 已选工厂模型 —— Node SelectedFacility
        /// <summary>
        /// 已选工厂模型
        /// </summary>
        [DependencyProperty]
        public Node SelectedFacility { get; set; }
        #endregion

        #region 负责人 —— string Principal
        /// <summary>
        /// 负责人
        /// </summary>
        [DependencyProperty]
        public string Principal { get; set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        [DependencyProperty]
        public string Description { get; set; }
        #endregion

        #region 设备列表 —— ObservableCollection<EquipmentInfo> Equipments
        /// <summary>
        /// 设备列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<EquipmentInfo> Equipments { get; set; }
        #endregion

        #endregion

        #region # 方法

        //Initializations

        #region 加载 —— void Load(IEnumerable<EquipmentInfo> equipments)
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="equipments">设备列表</param>
        public void Load(IEnumerable<EquipmentInfo> equipments)
        {
            this.Equipments = new ObservableCollection<EquipmentInfo>(equipments);
        }
        #endregion


        //Actions

        #region 选择工厂模型 —— async void SelectFacility()
        /// <summary>
        /// 选择工厂模型
        /// </summary>
        public async void SelectFacility()
        {
            this.Busy();

            SelectFacilityViewModel viewModel = ResolveMediator.Resolve<SelectFacilityViewModel>();
            bool? result = await this._windowManager.ShowDialogAsync(viewModel);
            if (result == true)
            {
                this.SelectedFacility = viewModel.SelectedFacility;
            }

            this.Idle();
        }
        #endregion

        #region 清空工厂模型 —— async void ClearFacility()
        /// <summary>
        /// 清空工厂模型
        /// </summary>
        public async void ClearFacility()
        {
            this.SelectedFacility = null;
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证

            if (string.IsNullOrWhiteSpace(this.EquipmentAssetNo))
            {
                MessageBox.Show("设备资产编号不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (string.IsNullOrWhiteSpace(this.EquipmentAssetName))
            {
                MessageBox.Show("设备资产名称不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (this.SelectedEquipment == null)
            {
                MessageBox.Show("设备不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (this.ProducedDate == null)
            {
                MessageBox.Show("生产日期不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (this.PurchasedDate == null)
            {
                MessageBox.Show("采购日期不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (this.Worth == null)
            {
                MessageBox.Show("价值不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            #endregion

            this.Busy();

            await Task.Run(() => this._maintenanceContract.Channel.CreateEquipmentAsset(this.EquipmentAssetNo, this.EquipmentAssetName, this.SelectedEquipment.Id, this.ProducedDate.Value, this.PurchasedDate.Value, this.EnabledDate, this.Manufacturer, this.Specification, this.ModelNo, this.Worth.Value, this.ServiceLife, this.DepreciableLife, this.SupportPhone, this.SelectedFacility?.Id, this.Principal, this.Description));

            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion

        #endregion
    }
}
